<?php
/**
 * @author: wangxiwen
 * @date: 2021/3/4 10:50 上午
 */

namespace App\Http\Services;


use App\Http\Model\FinanceDiamond;
use App\Http\Model\Friends;
use App\Http\Model\GiftBag;
use App\Http\Model\RoomHostMicro;
use App\Http\Model\RoomMicro;
use App\Http\Model\RoomUser;

class ApiServices extends CommonServices
{
    protected $financeDiamond;
    protected $giftBag;
    protected $roomUser;
    protected $roomHostMicro;
    protected $friends;
    protected $roomMicro;

    public function __construct()
    {
        parent::__construct();
        $this->financeDiamond = new FinanceDiamond();
        $this->giftBag = new GiftBag();
        $this->roomUser = new RoomUser();
        $this->roomHostMicro = new RoomHostMicro();
        $this->friends = new Friends();
        $this->roomMicro = new RoomMicro();
    }

    /**
     * 增加账户钻石
     * @param $userId
     * @param $type
     * @param $amount
     * @param $description
     * @return bool
     */
    public function increase($userId, $type, $amount, $description)
    {
        $amount = $amount < 0 ? abs($amount) : $amount;
        $res = $this->financeDiamond->inc((int)$userId, 0, 0, (int)$type, (int)$amount, (string)$description);
        return $res;
    }

    /**
     * 扣减账户钻石
     * @param $userId
     * @param $type
     * @param $amount
     * @param $description
     * @return bool
     */
    public function decrease($userId, $type, $amount, $description)
    {
        $amount = $amount > 0 ? -$amount : $amount;
        $res = $this->financeDiamond->inc((int)$userId, 0, 0, (int)$type, (int)$amount, (string)$description);
        return $res;
    }

    /**
     * 发放礼物到礼物背包
     * @param $userId
     * @param $giftId
     * @param $giftNum
     * @return bool
     */
    public function sendGift($userId , $giftId, $giftNum, $type)
    {
        $giftNum = $giftNum < 0 ? abs($giftNum) : $giftNum;
        $res = $this->giftBag->inc((int)$userId, 0, (int)$giftId, (int)$giftNum, (int)$type);
        return $res;
    }

    /**
     * 直播房间(在麦序上的用户所在房间)
     * @param array $userIds
     * @return array
     */
    public function liveRooms($userIds = [])
    {
        $data = [];
        foreach($userIds as $userId) {
            //用户所在房间
            $roomId = $this->roomUser->get_user_cur_room($userId);
            if($roomId === null) continue;
            //房间主持麦
            $hostId = $this->roomHostMicro->get_user_id($roomId);
            if($userId == $hostId) {
                $data[$userId] = $roomId;
                continue;
            }
            //房间普通麦用户状态
            $status = $this->roomMicro->get_user_micro_status($roomId, $userId);
            if($status != null) {
                $data[$userId] = $roomId;
                continue;
            }
        }
        return $data;
    }

    /**
     * 关注列表
     * @param $userId
     * @param array $followUserIds
     * @return array
     */
    public function followList($userId, $followUserIds = [])
    {
        $data = [];
        foreach($followUserIds as $followUserId) {
            if($followUserId == $userId) {
                $data[$followUserId] = 1;
                continue;
            }
            $res = $this->friends->get_follow_exist($userId, $followUserId);
            $data[$followUserId] = $res ? 1 : 0;
        }
        return $data;
    }

    /**
     * 添加关注
     * @param $userId
     * @param array $followUserId
     * @return array
     */
    public function addFollow($userId, $followUserId)
    {
        $res = $this->friends->add_follow($userId, $followUserId);
        return !$res ? false : true;
    }
}
